package com.itheima.controller;

import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
@Api (tags = "管理端检查项管理相关接口")
@Slf4j
public class CheckItemController {
    @Autowired
    private CheckItemService checkItemService;
    /**
     * 新增检查项
     * @param checkItem
     * @return
     */
    @PostMapping  ("/add")
    @ApiOperation("新增检查项")
    public Result add(@RequestBody CheckItem checkItem){
        log.info("新增检查项：{}",checkItem);
        checkItemService.add(checkItem);
        return new Result(true,"新增检查项成功");
    }

    /**
     * 删除检查项
     * @param id
     * @return
     */
    @GetMapping("/delete")
    @ApiOperation("删除检查项")
    public Result deleteById(Integer id){
        log.info("删除检查项：{}",id);
        checkItemService.deleteById(id);
        return new Result(true,"删除检查项成功");
    }

    /**
     * 根据id查询检查项
     * @param id
     * @return
     */
    @GetMapping("/findById")
    @ApiOperation("根据id查询检查项")
    public Result findById(Integer id){
        log.info("查询检查项：{}",id);
        CheckItem checkItem = checkItemService.findById(id);
        return new Result(true,"查询检查项成功",checkItem);
    }

    /**
     * 编辑检查项
     * @param checkItem
     * @return
     */
    @PostMapping("/edit")
    @ApiOperation("编辑检查项")
    public Result edit (@RequestBody CheckItem checkItem){
        log.info("编辑检查项：{}",checkItem);
        checkItemService.edit(checkItem);
        return new Result(true,"编辑检查项成功");
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    @ApiOperation("分页查询")
    public PageResult pageQuery(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询：{}",queryPageBean);
        PageResult pageResult = checkItemService.pageQuery(queryPageBean);
        return pageResult;
    }

    @GetMapping("/findAll")
    @ApiOperation("查询所有检查项")
    public Result findAll(){
        log.info("查询所有检查项");
        List<CheckItem> checkItemList = checkItemService.findAll();
        return new Result(true,"查询所有检查项成功",checkItemList);
    }

    @GetMapping("/findChechItemIdByCheckGroupId")
    @ApiOperation("根据检查组id查询检查项id")
    public Result findChechItemIdByCheckGroupId(Integer checkGroupId){
        log.info("根据检查组id查询检查项id：{}",checkGroupId);
        List<Integer> checkItemIdList = checkItemService.findChechItemIdByCheckGroupId(checkGroupId);
        return new Result(true,"查询检查项成功",checkItemIdList);
    }
}
